<html>
<head><meta charset="utf-8"><title>Unsized coercion appears twice in MIR? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html">Unsized coercion appears twice in MIR?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="183168018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183168018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183168018">(Dec 11 2019 at 15:31)</a>:</h4>
<p>To my surprise, the following code</p>
<div class="codehilite"><pre><span></span><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">rc</span>::<span class="n">Rc</span><span class="p">;</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nc">Rc</span><span class="o">&lt;</span><span class="nb">String</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Rc</span><span class="o">&lt;</span><span class="n">dyn</span><span class="w"> </span><span class="nb">Send</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>generates MIR like this:</p>
<div class="codehilite"><pre><span></span>fn  foo(_1: std::rc::Rc&lt;std::string::String&gt;) -&gt; std::rc::Rc&lt;dyn std::marker::Send&gt; {
    let mut _0: std::rc::Rc&lt;dyn std::marker::Send&gt;; // return place in scope 0 at src/lib.rs:2:26: 2:38
    let mut _2: std::rc::Rc&lt;dyn std::marker::Send&gt;; // in scope 0 at src/lib.rs:2:41: 2:42
    let mut _3: std::rc::Rc&lt;std::string::String&gt;; // in scope 0 at src/lib.rs:2:41: 2:42

    bb0: {
        StorageLive(_2);                 // bb0[0]: scope 0 at src/lib.rs:2:41: 2:42
        StorageLive(_3);                 // bb0[1]: scope 0 at src/lib.rs:2:41: 2:42
        _3 = move _1;                    // bb0[2]: scope 0 at src/lib.rs:2:41: 2:42
        _2 = move _3 as std::rc::Rc&lt;dyn std::marker::Send&gt; (Pointer(Unsize)); // bb0[3]: scope 0 at src/lib.rs:2:41: 2:42
        StorageDead(_3);                 // bb0[4]: scope 0 at src/lib.rs:2:41: 2:42
        _0 = move _2 as std::rc::Rc&lt;dyn std::marker::Send&gt; (Pointer(Unsize)); // bb0[5]: scope 0 at src/lib.rs:2:41: 2:42
        StorageDead(_2);                 // bb0[6]: scope 0 at src/lib.rs:2:43: 2:44
        return;                          // bb0[7]: scope 0 at src/lib.rs:2:44: 2:44
    }
}
</pre></div>


<p>why does it cast twice? Cc <span class="user-mention" data-user-id="124288">@oli</span> <span class="user-mention" data-user-id="119009">@eddyb</span></p>



<a name="183168958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183168958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183168958">(Dec 11 2019 at 15:39)</a>:</h4>
<p>hm, I wonder if we unsize whenever we move an unsized value</p>



<a name="183169066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183169066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183169066">(Dec 11 2019 at 15:40)</a>:</h4>
<p><code>Rc&lt;dyn Send&gt;</code> isn't an unsized value though</p>



<a name="183169167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183169167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183169167">(Dec 11 2019 at 15:41)</a>:</h4>
<p>er, yeah, poor phrasing on my part unsized "type" or so, not sure if we have a good name</p>



<a name="183169206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183169206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183169206">(Dec 11 2019 at 15:41)</a>:</h4>
<p>e.g. <code>fn foo(x: Rc&lt;String&gt;) -&gt; Rc&lt;dyn Send&gt; { let v: Rc&lt;dyn Send&gt; = x; v }</code> produces:</p>
<div class="codehilite"><pre><span></span>fn  foo(_1: std::rc::Rc&lt;std::string::String&gt;) -&gt; std::rc::Rc&lt;dyn std::marker::Send&gt; {
    debug x =&gt; _1;                       // in scope 0 at src/lib.rs:2:8: 2:9
    let mut _0: std::rc::Rc&lt;dyn std::marker::Send&gt;; // return place in scope 0 at src/lib.rs:2:26: 2:38
    let mut _2: std::rc::Rc&lt;dyn std::marker::Send&gt;; // in scope 0 at src/lib.rs:2:66: 2:67
    let _3: std::rc::Rc&lt;dyn std::marker::Send&gt; as UserTypeProjection { base: UserType(0), projs: [] }; // in scope 0 at src/lib.rs:2:45: 2:46
    let mut _4: std::rc::Rc&lt;std::string::String&gt;; // in scope 0 at src/lib.rs:2:63: 2:64
    let mut _5: std::rc::Rc&lt;dyn std::marker::Send&gt;; // in scope 0 at src/lib.rs:2:66: 2:67
    scope 1 {
        debug v =&gt; _3;                   // in scope 1 at src/lib.rs:2:45: 2:46
    }

    bb0: {
        StorageLive(_2);                 // bb0[0]: scope 0 at src/lib.rs:2:66: 2:67
        StorageLive(_3);                 // bb0[1]: scope 0 at src/lib.rs:2:45: 2:46
        StorageLive(_4);                 // bb0[2]: scope 0 at src/lib.rs:2:63: 2:64
        _4 = move _1;                    // bb0[3]: scope 0 at src/lib.rs:2:63: 2:64
        _3 = move _4 as std::rc::Rc&lt;dyn std::marker::Send&gt; (Pointer(Unsize)); // bb0[4]: scope 0 at src/lib.rs:2:63: 2:64
        StorageDead(_4);                 // bb0[5]: scope 0 at src/lib.rs:2:63: 2:64
        StorageLive(_5);                 // bb0[6]: scope 1 at src/lib.rs:2:66: 2:67
        _5 = move _3;                    // bb0[7]: scope 1 at src/lib.rs:2:66: 2:67
        _2 = move _5 as std::rc::Rc&lt;dyn std::marker::Send&gt; (Pointer(Unsize)); // bb0[8]: scope 1 at src/lib.rs:2:66: 2:67
        StorageDead(_5);                 // bb0[9]: scope 1 at src/lib.rs:2:66: 2:67
        StorageDead(_3);                 // bb0[10]: scope 0 at src/lib.rs:2:68: 2:69
        _0 = move _2 as std::rc::Rc&lt;dyn std::marker::Send&gt; (Pointer(Unsize)); // bb0[11]: scope 0 at src/lib.rs:2:66: 2:67
        StorageDead(_2);                 // bb0[12]: scope 0 at src/lib.rs:2:68: 2:69
        return;                          // bb0[13]: scope 0 at src/lib.rs:2:69: 2:69
    }
}
</pre></div>



<a name="183169226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183169226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183169226">(Dec 11 2019 at 15:42)</a>:</h4>
<p>which casts another time</p>



<a name="183171847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183171847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183171847">(Dec 11 2019 at 16:06)</a>:</h4>
<p>Clearly this is a job for the MIR optimization work :-)</p>



<a name="183173193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183173193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183173193">(Dec 11 2019 at 16:19)</a>:</h4>
<p>lmao</p>



<a name="183173252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183173252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183173252">(Dec 11 2019 at 16:20)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> this is <code>Rc&lt;T&gt;</code> -&gt; <code>Rc&lt;dyn Trait&gt;</code> -&gt; <code>Rc&lt;dyn Trait&gt;</code></p>



<a name="183173257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183173257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183173257">(Dec 11 2019 at 16:20)</a>:</h4>
<p>the second one is a noop</p>



<a name="183173323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183173323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183173323">(Dec 11 2019 at 16:21)</a>:</h4>
<p>try <code>fn foo(x: Rc&lt;dyn Send&gt;) -&gt; Rc&lt;dyn Send&gt; { x }</code></p>



<a name="183173366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183173366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183173366">(Dec 11 2019 at 16:21)</a>:</h4>
<p>the problem is that this might actually be needed by borrowck in some cases hmpf</p>



<a name="183173370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183173370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183173370">(Dec 11 2019 at 16:21)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="116118">@Matthew Jasper</span></p>



<a name="183178686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183178686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183178686">(Dec 11 2019 at 17:15)</a>:</h4>
<p>It's needed in borrowck because <code>&amp;mut Send + 'static</code> -&gt; <code>&amp;mut Send + 'a</code> is a valid coercion, even though <code>&amp;mut T</code> is invariant in <code>T</code>. It does seem a bit excessive to generate two casts in the case in the OP though.</p>



<a name="183254986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183254986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183254986">(Dec 12 2019 at 12:47)</a>:</h4>
<p><span class="user-mention" data-user-id="116118">@Matthew Jasper</span> maybe we can detect the case where it's <code>'static -&gt; 'static</code>?</p>



<a name="183254989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20coercion%20appears%20twice%20in%20MIR%3F/near/183254989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20coercion.20appears.20twice.20in.20MIR.3F.html#183254989">(Dec 12 2019 at 12:47)</a>:</h4>
<p>like in the OP</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>